package cn.t.converter.order;


import cn.t.constants.CommonConstants;
import cn.t.core.converter.CoreDTOConverter;
import cn.t.dto.api.CustomerItemDTO;
import cn.t.dto.api.WorkOrderCreateRequestDTO;
import cn.t.dto.api.WorkOrderStatusUpdateRequestDTO;
import cn.t.dto.order.ExcelWorkOrderDTO;
import cn.t.dto.order.WorkOrderCreateDTO;
import cn.t.dto.order.WorkOrderDTO;
import cn.t.enums.order.WorkOrderTypeEnum;
import cn.t.model.order.OrderDO;
import cn.t.model.order.WorkOrderDO;
import cn.t.model.order.WorkOrderDetailDO;
import cn.t.model.sku.SkuDO;
import cn.t.request.WorkOrderReq;
import cn.t.utils.DateUtil;
import cn.t.vo.andriod.AndroidWorkOrderVO;
import cn.t.vo.applet.AppletWorkOrderDetailVO;
import cn.t.vo.applet.AppletWorkOrderVO;
import cn.t.vo.ent.EntWorkOrderDetailVO;
import cn.t.vo.order.WorkOrderVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 工单表(WorkOrder)dto转domain
 *
 * @author t
 * @since 2024-05-24 10:19:35
 */
@Mapper(componentModel = "spring")
public interface WorkOrderDTOConverter extends CoreDTOConverter<WorkOrderDTO, WorkOrderDO> {

    @Mapping(target = "nickname", source = "creator")
    WorkOrderDO req2Do(WorkOrderReq request);

    @Mapping(target = "workOrderId", source = "reconDO.id")
    @Mapping(target = "creator", expression = "java(getName(reconDO.getCreator(), reconDO.getNickname()))")
    WorkOrderVO do2Vo(WorkOrderDO reconDO, String firmName, String costCenterName);

    @Mapping(target = "workOrderSn", source = "workOrderDO.workOrderSn")
    @Mapping(target = "creator", source = "workOrderDO.creator")
    @Mapping(target = "gmtCreated", source = "workOrderDetailDO.gmtCreated")
    ExcelWorkOrderDTO data2ExcelDto(WorkOrderDO workOrderDO, WorkOrderDetailDO workOrderDetailDO, Date pushTime, String firmName, String costCenterName);

    // nidec 创建工单入参转换
    @Mapping(target = "account", source = "creatorId")
    WorkOrderCreateDTO createReq2Model(WorkOrderCreateRequestDTO request);
    // 企业站更新状态入参转换
    WorkOrderDO updateReq2Do(WorkOrderStatusUpdateRequestDTO request, Long firmId);
    // 创建工单dto 转换为do
    @Mapping(target = "gmtExpire", source = "workOrderCreateDTO.expireDate")
    @Mapping(target = "docType", source = "workOrderCreateDTO.type")
    @Mapping(target = "modifier", source = "creator")
    @Mapping(target = "creator", source = "creator")
    WorkOrderDO createDto2Do (WorkOrderCreateDTO workOrderCreateDTO, String creator);
    // 创建工单dto 转换为detailDo
    @Mapping(target = "customerSkuCode", source = "item.itemId")
    @Mapping(target = "skuCode", source = "item.sku")
    @Mapping(target = "planQty", source = "item.qty")
    @Mapping(target = "status", constant = "1")
    WorkOrderDetailDO createDto2DetailDo (WorkOrderCreateDTO.WorkOrderCreateItem item,WorkOrderCreateDTO order, String skuName, String creator, String skuImg);

    // 转换为设备端工单详情
    @Mapping(target = "workOrderDetailId", source = "workOrderDetailDO.id")
    @Mapping(target = "status", source = "workOrderDetailDO.status")
    @Mapping(target = "skuImg", source = "sku.skuImg")
    @Mapping(target = "skuName", source = "sku.skuName")
    @Mapping(target = "skuCode", source = "sku.skuCode")
    AndroidWorkOrderVO.WorkOrderDetail data2AndroidWorkOrderDetail(WorkOrderDetailDO workOrderDetailDO, boolean available, SkuDO sku, String cargowayNos);

    EntWorkOrderDetailVO do2EntDetail(WorkOrderDO workOrderDO, List<EntWorkOrderDetailVO.SkuInfo> items);

    EntWorkOrderDetailVO.SkuInfo do2EntDetailSku(WorkOrderDetailDO workOrderDetailDO, List<String> skuLocations);


    @Mapping(target = "workOrderId", source = "workOrderDO.id")
    @Mapping(target = "expireDate", source = "workOrderDO.gmtExpire")
    @Mapping(target = "createDate", source = "workOrderDO.gmtCreated")
    @Mapping(target = "jobNum", source = "workOrderDO.empNo")
    @Mapping(target = "name", source = "workOrderDO.nickname")
    AppletWorkOrderDetailVO data2AppletDetailVo(WorkOrderDO workOrderDO, List<AppletWorkOrderDetailVO.WorkOrderSku> skuList);

    AppletWorkOrderDetailVO.WorkOrderSku data2AppletDetailSku(WorkOrderDetailDO workOrderDetailDO, List<AppletWorkOrderDetailVO.Device> devices);

    default String getName(String username, String nickname) {
        return StringUtils.hasText(nickname) ? nickname : username;
    }

    default String getRealSkuInfo(String originalData, String customerData) {
        return StringUtils.hasText(customerData) ? customerData : originalData;
    }

    default Long getUserId(String creator) {
        return StringUtils.hasText(creator) ? Long.valueOf(creator.split(" ")[0]) : null;
    }

    default void data2Do(OrderDO orderDO, WorkOrderDO workOrderDTO ) {

        workOrderDTO.setCostCenterId(orderDO.getCostCenterId());

        workOrderDTO.setCardNo(orderDO.getCardCode());
        workOrderDTO.setNickname(orderDO.getUserName());
        workOrderDTO.setFirmId(orderDO.getFirmId());
        workOrderDTO.setDocType(WorkOrderTypeEnum.PICK.getCode());
        workOrderDTO.setRemark("系统创建");
        workOrderDTO.setGmtExpire(DateUtil.afterTime(LocalDateTime.now(), CommonConstants.SEVEN, TimeUnit.DAYS));
        workOrderDTO.setWorkOrderSource(CommonConstants.TWO);
        workOrderDTO.setModifier(orderDO.getModifier());
        workOrderDTO.setCreator(orderDO.getCreator());
    }
}

